package com.hrt.druidcode;

import java.sql.*;
import java.util.Properties;

/**
 *  使用JDBC的方式连接Druid
 */
public class ReadDruidData {
    public static void main(String[] args) throws SQLException {
        //1.连接Druid
        Properties props = new Properties();
        // 固定格式
        Connection conn = DriverManager.getConnection("jdbc:avatica:remote:url=http://node5:8888/druid/v2/sql/avatica/", props);

        //2.准备查询语句
        PreparedStatement pst = conn.prepareStatement("select loc,item,sum(amount) as total_amount from \"druid-topic\" group by loc,item");

        //3.执行查询获取结果
        ResultSet rst = pst.executeQuery();

        while(rst.next()){
            String loc = rst.getString("loc");
            String item = rst.getString("item");
            Double totalAmount = rst.getDouble("total_amount");
            System.out.println("loc = "+loc +",item = "+item+",totalAmount = "+totalAmount);
        }

        //4.关闭连接
        rst.close();
        pst.close();
        conn.close();
    }
}
